/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elderberryni2.data;

import elderberryni2.enums.NetworkType;
import java.io.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author warringer
 */
public class ElderberryProperties {
    private String filename = "ElderberryNi.ini";
    private Properties props;
    private NetworkType networktype = NetworkType.UDP;
    private String ipAdress = "127.0.0.1";
    private int timePeriodSave = 7200;
    private int savesPerSecond = 2;
    private int port = 54345;
    private boolean testEnabled = true;

    public ElderberryProperties() throws FileNotFoundException, IOException {
        props = new Properties();
        File file = new File(filename);
        if (!file.exists()) {    
            saveProps();
        } else {
            FileInputStream in = new FileInputStream(file);
            props.load(in);
            in.close();
            networktype = NetworkType.valueOf(props.getProperty("NetworkType", networktype.toString()));
            ipAdress = props.getProperty("IP_Adress", ipAdress);
            port = Integer.parseInt(props.getProperty("Port", new Integer(port).toString()));
            testEnabled = Boolean.parseBoolean(props.getProperty("Tests_Enabled", new Boolean(testEnabled).toString()));
            timePeriodSave = Integer.parseInt(props.getProperty("Saveable_Duration", new Integer(timePeriodSave).toString()));
            savesPerSecond = Integer.parseInt(props.getProperty("Saves_per_Second", new Integer(savesPerSecond).toString()));
        }
    }
    
    public void saveProps() {
        File file = new File(filename);
        FileOutputStream out = null;
        try {
            props.setProperty("NetworkType", networktype.toString());
            props.setProperty("IP_Adress", ipAdress);
            props.setProperty("Port", new Integer(port).toString());
            props.setProperty("Tests_Enabled", new Boolean(testEnabled).toString());
            props.setProperty("Saveable_Duration", new Integer(timePeriodSave).toString());
            props.setProperty("Saves_per_Second", new Integer(savesPerSecond).toString());
            out = new FileOutputStream(file);
            props.store(out, "Properties");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ElderberryProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ElderberryProperties.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
           try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(ElderberryProperties.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String getIpAdress() {
        return ipAdress;
    }

    public void setIpAdress(String ipAdress) {
        this.ipAdress = ipAdress;
    }

    public NetworkType getNetworktype() {
        return networktype;
    }

    public void setNetworktype(NetworkType networktype) {
        this.networktype = networktype;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public boolean isTestEnabled() {
        return testEnabled;
    }

    public void setTestEnabled(boolean testEnabled) {
        this.testEnabled = testEnabled;
    }

    public void setSavesPerSecond(int savesPerSecond) {
        this.savesPerSecond = savesPerSecond;
    }

    public void setTimePeriodSave(int timePeriodSave) {
        this.timePeriodSave = timePeriodSave;
    }

    public int getSavesPerSecond() {
        return savesPerSecond;
    }

    public int getTimePeriodSave() {
        return timePeriodSave;
    }
}
